<template>
    <div class="main">
        <el-page-header @back="goBack" style="margin-bottom: 20px;" />
        <el-steps :active="active" finish-status="success" style="width: 1000px;margin: 0 auto">
            <el-step title="1.选择案件类型"></el-step>
            <el-step title="2.利益冲突检查"></el-step>
            <el-step title="3.新增委托人"></el-step>
            <el-step title="4.登记立案信息"></el-step>
            <el-step title="5.立案文书上传"></el-step>
            <el-step title="6.收案审批"></el-step>
        </el-steps>
        <el-divider></el-divider>
        <case-type v-show="active===0" :law="law" @setType="handleSetType"></case-type>
        <case-exam v-show="active===1" :law="law" @setCard="handleSetCard"></case-exam>
        <client-add v-show="active===2" :law="law" :active="active" :status="'add'" @add="handleSetClient"></client-add>
        <case-register v-show="active===3 && cost===0" :active="active" :cost="cost" :law="law" @submitLaw="handleSubmitLaw"></case-register>
        <feiyong v-show="active===3 && cost===1" :law="law" :active="active" :cost="cost" @completed="handleCompleted"></feiyong>
        <document v-show="active===4" :law="law" @completed="handleCompleted"></document>
        <case-close v-show="active===5" :caseid="law.id" :active="active"></case-close>
    </div>
</template>

<script>
    //引入选择案件类型组件
    const CaseType = () => import('./CaseType.vue')
    //引入利益冲突审查页面
    const CaseExam = () => import('../CaseExam/CaseExam.vue')
    //引入新增客户组件
    const ClientAdd = () => import('../Client/ClientAdd.vue')
    //引入立案组件
    const CaseRegister = () => import('./CaseRegister.vue')
    //引入费用减免组件
    const Feiyong = () => import('../Feiyong/FeiyongAdd.vue')
    //引入文书上传组件
    const Document = () => import('./Document.vue')
    //引入收案审批组件
    const CaseClose = () => import('./CaseClose.vue')
    //引入案件服务类
    import CaseLawService from '../../../model/gs/CaseLawService.js'
    //引入客户服务类
    import ClientService from '../../../model/gs/ClientService.js'

    //拿到ClientService实例
    const clientService = ClientService.getInstance()
    //拿到CaseLawService实例
    const caseLawService = CaseLawService.getInstance()

    export default {
        data() {
            return {
                law:{
                    id:'',
                    caseOppositeCardnum:'',
                    cardNum:'',
                    caseLawsuitobj:'',
                    caseAccept:'公安机关',
                    caseAgencyfee:0,
                    caseChargeWay:'',
                    caseFxmoney:0,
                    caseSubsidy:'N',
                    caseSubsidysal:0,
                    caseStatus:'',
                    caseLawsuitStatus:'原告',
                    caseProxysal:0,
                    caseAssign:'N',
                    caseWtr:'',
                    caseTransactionRegion:'省/直辖市地级区'
                },
                active:0,
                cost:0
            }
        },
        methods:{
            goBack(){
                if(this.active>0){
                    if(this.cost === 1){
                        this.cost--
                    } else{
                        this.active--
                    }
                } else{
                    history.back()
                }
            },
            handleSetType(id,caseTypeName,typeMoneyMin,parentId){
                this.law.caseCaseTypeId = id
                this.law.caseNo = caseTypeName
                this.law.caseLawsuitobj = typeMoneyMin
                //查询出最大编号并赋值
                caseLawService.getCode().then(response => {
                    if(response.data.code === 0){
                        let num = response.data.data
                        if(num < 10){
                            num = '0'+num
                        }
                        this.law.caseNo =
                            new Date().getFullYear()
                            +this.law.caseNo+'第00'
                            +num+'号'
                        //判断父id决定是否要例如冲突审查
                        if(parentId == 1004 || parentId == 1005 || parentId == 1006){
                            this.active+=2
                        }else{
                            //跳转到审查页面
                            this.active++
                        }
                    }
                })
            },
            handleSetCard(params){
                //设置身份证号到law对象
                for (let key in params){
                    this.law[key] = params[key]
                }
                //通过身份证验证是否已经存在该客户
                clientService.list({cardNum:this.law.cardNum}).then(response => {
                    if(response.data.data.length !== 0){
                        this.$message({
                            message:'该客户已经存在了，将直接立案',
                            duration:1000
                        })
                        this.law.clientId = response.data.data[0].id
                        this.law.caseWtr = response.data.data[0].client
                        this.active+=2
                    }else {
                        this.active++
                    }
                })
            },
            //设置客户的方法
            handleSetClient(client){
                if(!clientService.check(client)){
                    return
                }
                //查询是否存在该客户
                clientService.list({cardNum:this.law.cardNum}).then(response => {
                    if(response.data.data.length !== 0){
                        this.$message({
                            message:'该客户已经存在了，将直接立案',
                            duration:1000
                        })
                        this.law.clientId = response.data.data[0].id
                        this.law.caseWtr = response.data.data[0].client
                        this.active++
                    }else {
                        //拼接地址
                        client.pertainarea = client.province + client.city
                        //发送请求添加客户
                        clientService.save(client).then(response => {
                            if (response.data.code === 0) {
                                this.$message.success({
                                    message: '添加成功',
                                    duration: 1000
                                })
                                this.law.clientId = client.id
                                this.law.caseWtr = client.client
                                //跳转立案页面
                                this.active++
                            } else {
                                this.$message.error({
                                    message: response.data.message,
                                    duration: 1000
                                })
                            }
                        })
                    }
                })
            },
            //提交案件的方法
            handleSubmitLaw(cwPerformancesplitList){
                //校验数据是否合法
                if(!caseLawService.check(this,this.law,cwPerformancesplitList)){
                    return false
                }
                //调用方法保存案件
                caseLawService.save(this.law,cwPerformancesplitList).then(response => {
                    if(response.data.code === 0){
                        const flag = this.law.caseAgencyfee < this.law.caseLawsuitobj
                        this.law = response.data.data
                        //判断代理费是否小于标的
                        if(flag){
                            this.$message.warning({
                                message:'代理费低于最低标的，将申请费用减免',
                                duration:1000
                            })
                            //切换到费用减免申请页面
                            this.cost++
                        }else{
                            //切换到文书上传页面
                            this.active++
                        }
                    }
                })
            },
            //费用减免申请完成的方法
            handleCompleted(){
                this.active++
            }
        },
        created(){
            if(this.$route.params.active !=null && this.$route.params.active!=undefined){
                this.active = this.$route.params.active
                this.cost = this.$route.params.cost
                this.law = this.$route.params.law
            }
        },
        components:{
            ClientAdd,
            CaseType,
            CaseExam,
            CaseRegister,
            Feiyong,
            Document,
            CaseClose
        }
    }
</script>

<style scoped>
    .main{
        width: 100%;
        height: 100%;
    }
</style>
